package com.example.boos.ui.notifications;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.boos.HttpUtil.MyPost;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.pojo.Orders;
import com.example.boos.util.MyThreadPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NotificationsFragment extends Fragment {

    private  View view ;
    public static List<Orders> data ;
    private  ListView ordersListView;
    public static orderItemNotiAdapter orderItemNotiAdapter;

    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    // 通知更新
                    orderItemNotiAdapter.updateAll(data);
                    break;
            }
        }
    };

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

            view = inflater.inflate(R.layout.fragment_notifications, null, false);
            // 获取数据
            initData();
            ordersListView = view.findViewById(R.id.in_orders);
            orderItemNotiAdapter = new orderItemNotiAdapter(data,this.getContext());
            ordersListView.setAdapter(orderItemNotiAdapter);
        return view;
    }

    private void initData(){
        if(data == null){
            // 获取到订单数据 根据 storeId 和  Storestatus为0
            MyThreadPool.doJob(new Runnable() {
                @Override
                public void run() {
                    String url = "getOrdersByStoreStatus";
                    Map<String,Integer> param = new HashMap<>();
                    param.put("storeId", BossInfo.currentStoreId);
                    param.put("storeStatus",0);
                    data = (List<Orders>) MyPost.doPost(url,param);
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
            });
        }else{
            orderItemNotiAdapter.updateAll(data);
        }

    }
}